package com.attendance.sz.db;

import java.util.List;

import com.attendance.sz.utils.MyApplication;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;



public abstract class DBHelper extends SQLiteOpenHelper {

	// database table create statement
	private List<String> databaseCreate;

	protected DBHelper(String dbName, int dbVersion, List<String> databaseCreate)
	{
		super(MyApplication.getAppContext(), dbName, null, dbVersion);
		this.databaseCreate = databaseCreate;
	}
	
	protected DBHelper(String dbName, int dbVersion, List<String> databaseCreate, Context context) 
	{
		super(context, dbName, null, dbVersion);
		this.databaseCreate = databaseCreate;
	}

	/**
	 * structure
	 * 
	 * @param name     
	 * @param version
	 * @param databaseCreate
	 * @param context  
	 * @param factory  
	 */
	protected DBHelper(String name, int version, List<String> databaseCreate, Context context, CursorFactory factory) 
	{
		super(context, name, factory, version);
		this.databaseCreate = databaseCreate;
	}

	@Override
	public void onCreate(SQLiteDatabase db) 
	{
		if (databaseCreate != null && db != null) {
			db.beginTransaction();
			try {
				for (String sql : databaseCreate) {
					db.execSQL(sql);
				}
				db.setTransactionSuccessful();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				db.endTransaction();
			}
		} else {
			throw new IllegalArgumentException(getClass().getSimpleName()
					+ "\tonCreate()");
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		UpgradeDB(db, oldVersion, newVersion);
	}
	
	protected abstract void UpgradeDB(SQLiteDatabase db, int oldVersion, int newVersion);

}
